from flask import jsonify
from plantmanagement.models import userplant
from plantmanagement.fun import Tools
from plantmanagement.models import Users


class UserPlantController:
    def addPlants(**self):

        plantname = self['plantname']
        plantsrc = self['plantsrc']
        userid = self['userid']
        introduce = self['introduce']

        if Tools.QueryUsers(userid):
            u = userplant(plantname=plantname, userid=userid,plantsrc=plantsrc,introduce=introduce)
            Tools.db_commit(u)
            s = userplant.query.filter_by(userid=userid).all()
            t = []
            for a in s:
                t.append(a.to_json())
            return t
        else:
            return {
                'code':201,
                'msg':'用户不存在'
            }

    def allPlants(**self):
        userid = self['userid']
        s = userplant.query.filter_by(userid=userid).all()
        t = []
        for a in s:
            t.append(a.to_json())
        return t

    def delPlants(**self):
        pass
